$(function(){
	
	
    //1.复制功能
	var $copy_items = $('.my_clip_button');
	$copy_items.mouseover(function(){
		if((typeof $(this).attr('inited')) == 'undefined'){		
			$(this).attr('inited','1');
			
			var id = GetPostfix($(this).attr('id'));
			var prefix = $(this).attr('prefix');
			copy_init(prefix,id);
		}
	});



    //2.显示弹出层问题

    $('.td_pop').hover(
        function(){
            $(this).find('.div_pop').show();
        },
        function(){
            $(this).find('.div_pop').hide();
        }
    );


    //3.设置异常订单
    var $is_exception = $('a[name="is_exception"]');

    $is_exception.click(function(){
        var order_id = $(this).attr('order_id');

        var is_exception = 0;
        if(!$(this).hasClass('color-red')){
            is_exception = 1;
        }
        //设置是否异常
        set_is_exception(order_id,is_exception);
    });


    //4.划入显示备注
    var $aRemark = $("a[name='is_exception']");

    //留言提示
    $aRemark.cluetip({
        cluetipClass: 'jtip',
        arrows: true,
        width: '300px',
        dropShadow: false,
        sticky: true,
        mouseOutClose: true,
        closePosition: 'title',
        readFromCache:false,
        closeText: '<img src="/Img/cluetip/cross.png" alt="close" />'
    });



    //5.修改后保存
    var $input = $('input[name="order_sn"],input[name="invoice_no"],input[name="goods_amount"],input[name="bonus_money"],input[name="shipping_fee"],input[name="paid_amount"],input[name="remark"]');

    //选中
    $input.hover(function(){
        $(this).select();
    });

    $input.change(function(){

        var task_id = $('#task_id').val();
        var id		= $(this).attr('id');
        var order_id= GetPostfix(id);
        var name 	= $(this).attr('name');
        var value 	= $.trim($(this).val());

        $(this).removeClass('bg-red').removeClass('bg-green').addClass('bg-white');

        var $txt = $(this);

        $.post('/index.php/Admin/Order/order_update',{
            'task_id'	:task_id,
            'order_id'	:order_id,
            'item'		:name,
            'value'		:value
        },function(data){

            $txt.removeClass('bg-white');

            if(data.success){
                $txt.removeClass('bg-red').addClass('bg-green');

                //更新下单ip
                if(data.data.order_ip != ''){
                    $('#order_ip-' + order_id).html(data.data.order_ip);
                }

                if(name == 'bonus_money' || name == 'paid_amount'){
                    //计算实际总金额
                    calc_real_price(order_id);
                }

            }else{
                $txt.removeClass('bg-green').addClass('bg-red');
                alert(data.message);
            }
        },'JSON');

    });



    //6.完成按钮
    var $btnFinish = $('.btnFinish');
    $btnFinish.click(function(){
        var $obj = $(this);
        var order_id = GetPostfix($obj.attr('id'));
        set_is_finish($obj,order_id);
    });




    //7.帐号
    var $btnResetMemberId = $('.btnResetMemberId');
    $btnResetMemberId.click(function(){

        var id = $(this).attr('id');
        var task_id = GetPostfix(id);

        var $checked_orders = $("#task-" + task_id + " input[name='check_orders']:checked");

        if($checked_orders.length == 0){
            alert('请先选择要重置的订单');
            return false;
        }

        if(!confirm('确认要重置分配的帐号?')){
            return false;
        }

        var orders = new Array();
        $checked_orders.each(function(i){
            var order_id = $($checked_orders[i]).val();
            orders.push(order_id);
        });

        $.post('/index.php/Admin/Task/reset_member_id',{
            'check_orders':orders.toString()
        },function(data){
            if(data.success){
                alert(data.message);
            }else{
                alert(data.message);
            }
            assign_member(task_id);
        },'JSON');
    });



    //8.留言提示
    $('a.jt').cluetip({
        cluetipClass: 'jtip',
        arrows: true,
        width: '600px',
        dropShadow: false,
        sticky: true,
        mouseOutClose: true,
        closePosition: 'title',
        readFromCache:false,
        closeText: '<img src="/Img/cluetip/cross.png" alt="close" />'
    });


});


//设置是否异常
function set_is_finish($obj,order_id){

    var $order_sn = $('#order_sn-' + order_id);
    var order_sn = $.trim($order_sn.val());


    var $bonus_money = $('#bonus_money-' + order_id);
    var bonus_money = $.trim($bonus_money.val());

    var $paid_amount = $('#paid_amount-' + order_id);
    var paid_amount = $.trim($paid_amount.val());

    var $shipping_fee = $('#shipping_fee-' + order_id);
    var shipping_fee = $.trim($shipping_fee.val());

    if(order_sn == ''){
        alert('请填写订单号');
        $order_sn.focus();
        return false;
    }

    $obj.attr('disabled',true).val('提交中');

    $.post('/index.php/Admin/Order/order_set_finish',{
        'order_id'	:order_id,
        'order_sn'	:order_sn,
        'bonus_money':bonus_money,
        'paid_amount':paid_amount,
        'shipping_fee':shipping_fee
    },function(data){

        if(data.success){
            $obj.parent().parent().html('<span class="color-green">已完成</span>');
            $('#order_ip-' + order_id).html(data.data.order_ip);
            //计算实际总金额
            calc_real_price(order_id);
        }else{
            alert('设置完成失败');
            $obj.attr('disabled',false).val('完成');
        }
    },'JSON');
}



//设置是否异常
function set_is_exception(order_id,is_exception){

    var $checked = $('#is_exception-' + order_id);

    $.post('/index.php/Admin/Order/order_update',{
        'order_id'	:order_id,
        'item'		:'is_exception',
        'value'		:is_exception
    },function(data){
        if(data.success){
            if(is_exception){
                $checked.attr('class','color-red').attr('title','异常订单');
            }else{
                $checked.attr('class','').attr('title','');
            }
        }else{
            alert(data.message);
        }
    },'JSON');
}



//计算实际总金额
function calc_real_price(id){

    var bonus_money = parseFloat($('#bonus_money-' + id).val());
    var paid_amount = parseFloat($('#paid_amount-' + id).val());

    if(isNaN(bonus_money)){
        bonus_money = 0;
    }
    if(isNaN(paid_amount)){
        paid_amount = 0;
    }

    var real_price = (bonus_money + paid_amount).toFixed(2);

    $('#real_price-' + id).html(real_price);
}





/**
 * 修改保存备注
 * @param taObj
 * @param order_id
 */
function order_remark_change(taObj,order_id){
    var remark = $.trim($(taObj).val());
    $.post('/index.php/Admin/Task/order_remark_update',{'order_id':order_id,'remark':remark},function(data){
        if(data.success){

            var $a = $('#is_exception-' + order_id);

            if(remark == ''){
                if($a.hasClass('bg-green')){
                    $a.removeClass('bg-green')
                }
            }else{
                if(!$a.hasClass('bg-green')){
                    $a.addClass('bg-green')
                }
            }

        }else{
            alert(data.message);
        }
    },'JSON');
}





//分配会员
function assign_member(task_id){

    $.post('/index.php/Admin/Task/assign_member',{
        'id':task_id
    },function(data){
        if(data.success){
            alert(data.message);
            location.reload(true);
        }else{
            alert(data.message);
        }
    },'JSON');
};



/********* 复制 *********/
function copy_init(prefix,id) {
    //复制功能
    var clip = null;

    clip = new ZeroClipboard.Client();
    clip.setHandCursor( true );


    clip.addEventListener('load', function (client) {
        //alert("Flash movie loaded and ready.");
    });

    clip.addEventListener('mouseOver', function (client) {

        var url = $('#' + prefix + '_clip_button-' + id).html();
        re = /&amp;/g;        // 创建正则表达式模式。
        url = url.replace(re, "&");   // 交换每一对单词。
        clip.setText(url);
    });

    clip.addEventListener('complete', function (client, text) {

        var $txt = $('#' + prefix + '_clip_button-' + id);

        if(!$txt.hasClass('bg-green')){
            $txt.addClass('bg-green');
        }

        //60秒后删除
        setTimeout(function(){
            $('#'+clip.movieId).remove();
        },3 * 60 * 1000);
    });

    clip.glue( prefix + '_clip_button-' + id, prefix + '_clip_container-' + id );
}

/*添加备注*/
function task_log_add(obj,task_id){

    var message = $('#message').val();

    if($.trim(message) == ''){
        alert('请填写备注');
        return false;
    }

    var $obj = $(obj);

    $obj.attr('disabled',true).val('提交中');

    $.post('/index.php/Admin/Task/task_log_add',{
        'task_id':task_id,
        'message':message
    },function(data){
        if(data.success){
            alert(data.message);
            location.reload(true);
        }else{
            alert(data.message);
        }

        $obj.attr('disabled',false).val('提交');

    },'JSON');
}